package com.baidumap.administrator.happystudy.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidumap.administrator.happystudy.R;

import java.util.List;

/**
 * author: C on 2017/3/21.
 * 购物车
 */
public class ShoppingPayAdapter extends BaseAdapter{

    private Context mContext;
    private List<Boolean> mList;
    private boolean flag = true;

    public ShoppingPayAdapter(Context context, List<Boolean> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_shopping, null);
            viewHolder.tvEdit = (TextView)convertView.findViewById(R.id.tv_edit);
            viewHolder.tvPriceOld = (TextView)convertView.findViewById(R.id.tv_price_old);
            viewHolder.ivChildChoose = (ImageView)convertView.findViewById(R.id.iv_child_choose);
            viewHolder.ivItemChoose = (ImageView)convertView.findViewById(R.id.iv_item_choose);
            viewHolder.llDetal = (LinearLayout)convertView.findViewById(R.id.ll_detal);
            viewHolder.rlDelete = (RelativeLayout)convertView.findViewById(R.id.rl_delete);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Boolean aBoolean = mList.get(position);
        viewHolder.ivItemChoose.setSelected(aBoolean);
        viewHolder.ivChildChoose.setSelected(aBoolean);
        Log.i("---", "size: -1--"+ aBoolean);

        //选择商品
        final ViewHolder finalViewHolder = viewHolder;
        viewHolder.ivItemChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (finalViewHolder.ivItemChoose.isSelected()) {
                    finalViewHolder.ivItemChoose.setSelected(false);
                    finalViewHolder.ivChildChoose.setSelected(false);
                    mList.remove(position);
                    mList.add(position, false);
                    mListener.onSeletedItem(false);

                } else {
                    mList.remove(position);
                    mList.add(position, true);
                    finalViewHolder.ivItemChoose.setSelected(true);
                    finalViewHolder.ivChildChoose.setSelected(true);
                    for (int i = 0; i < mList.size(); i++) {
                        if (mList.get(i) != true) {
                            flag = false;
                            break;
                        } else {
                            flag = true;
                        }
                    }
                    if (flag) {
                        mListener.onSeletedAllItem(true);
                    } else {
                        mListener.onSeletedAllItem(false);
                    }

                }

            }
        });
        viewHolder.ivChildChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (finalViewHolder.ivChildChoose.isSelected()) {
                    finalViewHolder.ivItemChoose.setSelected(false);
                    finalViewHolder.ivChildChoose.setSelected(false);
                    mList.remove(position);
                    mList.add(position, false);
                    mListener.onSeletedItem(false);
                } else {
                    finalViewHolder.ivItemChoose.setSelected(true);
                    finalViewHolder.ivChildChoose.setSelected(true);
                    mList.remove(position);
                    mList.add(position, true);
                    for (int i = 0; i < mList.size(); i++) {
                        if (mList.get(i) != true) {
                            flag = false;
                            break;
                        } else {
                            flag = true;
                        }
                    }
                    if (flag) {
                        mListener.onSeletedAllItem(true);
                    } else {
                        mListener.onSeletedAllItem(false);
                    }
                }
            }
        });
        //购物车编辑
        viewHolder.tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (finalViewHolder.tvEdit.getText().toString().equals("编辑")) {
                    finalViewHolder.tvEdit.setText("完成");
                    finalViewHolder.llDetal.setVisibility(View.GONE);
                    finalViewHolder.rlDelete.setVisibility(View.VISIBLE);
                } else {
                    finalViewHolder.tvEdit.setText("编辑");
                    finalViewHolder.llDetal.setVisibility(View.VISIBLE);
                    finalViewHolder.rlDelete.setVisibility(View.GONE);
                }
            }
        });
        viewHolder.tvPriceOld.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
//        mListener.seletedAll(viewHolder.ivItemChoose, viewHolder.ivChildChoose);
        return convertView;
    }

    class ViewHolder {
        private TextView tvEdit;
        private TextView tvPriceOld;
        private ImageView ivItemChoose;
        private ImageView ivChildChoose;
        private RelativeLayout rlDelete;
        private LinearLayout llDetal;
    }

    public interface SeletedListener {
        void onSeletedItem(boolean isSelected);
        void onSeletedAllItem(boolean isSelected);
    }
    private SeletedListener mListener;
    public void setOnSeletedAllItem(SeletedListener listener) {
        mListener = listener;
    }
    public void setOnSeletedItem(SeletedListener listener) {
        mListener = listener;
    }
}
